#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int m,n;

char map[210][210];
int cnt = 0;

void turn2D(int l, int t, int r, int b) {
	if (l>=r || t>=b) return;
	bool same = true;
	for (int i=l; i<r; ++i) {
		for (int j=t; j<b; ++j)
			if (map[j][i] != map[t][l]) {
				same = false;
				break;
			}
		if (!same) break;
	}

	if (same) {
		if (cnt == 50) {
			cnt = 0;
			cout << endl;
		}
		++cnt;
		cout << map[t][l];
		return;
	}
	else {
		if (cnt == 50) {
			cnt = 0;
			cout << endl;
		}
		++cnt;
		cout << 'D';
	}

	int w = (r-l+1)/2, h=(b-t+1)/2;
	turn2D(l,t,l+w,t+h);
	turn2D(l+w,t,r,t+h);
	turn2D(l,t+h,l+w,b);
	turn2D(l+w,t+h,r,b);
}

void turn2B(int l, int t, int r, int b) {
	if (l>=r || t>=b) return;
	char ch;
	scanf(" %c", &ch);
	if (ch == 'D') {
		int w = (r-l+1)/2, h=(b-t+1)/2;
		turn2B(l,t,l+w,t+h);
		turn2B(l+w,t,r,t+h);
		turn2B(l,t+h,l+w,b);
		turn2B(l+w,t+h,r,b);
	}
	else {
		for (int i=l; i<=r; ++i)
			for (int j=t; j<=b; ++j)
				map[j][i] = ch;
	}
}

int main(void)
{
	char ch;
	bool firstBlood = true;
	while (true) {
		scanf(" %c", &ch);
		if (ch == '#') break;
// 		if (firstBlood)  firstBlood = false;
// 		else cout << endl;
		if (ch == 'B') {
			scanf(" %d %d", &m, &n);
			for (int i=0; i<m; ++i)
				for (int j=0; j<n; ++j) {
					scanf(" %c", &map[i][j]);
				}

// 				for (int i=0; i<m; ++i) {
// 					for (int j=0; j<n; ++j)
// 						cout << map[i][j];
// 					cout << endl;
// 				}

			cout << "D" << setw(4) << right << m << setw(4) << right << n << endl;
			cnt = 0;
			turn2D(0,0,n,m);
		}
		else if (ch == 'D') {
			scanf(" %d %d", &m, &n);

			turn2B(0,0,n,m);

			cout << "B" << setw(4) << right << m << setw(4) << right << n << endl;
			cnt = 0;
			for (int i=0; i<m; ++i) {
				for (int j=0; j<n; ++j) {
					if (cnt == 50) {
						cout << endl;
						cnt = 0;
					}
					++cnt;
					cout << map[i][j];
				}
			}
		}

		cout << endl;
	}
	return 0;
}